package org.leo.dm.util;

import java.util.Random;
import java.util.UUID;

import org.leo.dm.util.EhcacheUtil;
import org.leo.dm.util.ThreadPoolSingletonUtil;
import org.leo.dm.util.ToUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

public class EhcacheExample {
	static Logger log = LoggerFactory.getLogger(EhcacheExample.class);

	public static int ei = 0;

	public static void main(String[] args) {
		try {
			// 初始化
			EhcacheUtil.init("/com/neusoft/dp/util/ehcache/ehcache.xml");
			ThreadPoolSingletonUtil.getInstance().initPool(10);
			for (int init = 0; init < 4; init++) {
				ThreadPoolSingletonUtil.getInstance().getPool().execute(new Runnable() {

					@Override
					public void run() {
						String cacheName = UUID.randomUUID().toString();
						cacheName = "qwe" + ei++;
						Cache cache = EhcacheUtil.getCache(cacheName);

						int inm = 0;
						int ind = 0;

						String ter = null;
						ToUtil t1 = new ToUtil();
						for (int i = 0; i < 50000; i++) {// 100000000
							cache.put(new Element("a" + i, UUID.randomUUID().toString()));
							ter = "a" + new Random().nextInt(999999);
							cache.get(ter);
							if (cache.isElementOnDisk(ter)) {
								ind++;
							} else {
								inm++;
							}

						}
						t1.takeTime("写", null);

						log.info("inm:" + inm + "--" + "ind:" + ind);

						ToUtil t2 = new ToUtil();
						// for (int i = 0; i < cache.getSize(); i++) {
						// //log.info();
						// cache.get("a" + i);
						// }
						t2.takeTime("读", null);

						ToUtil tx = new ToUtil();
						// log.info(cache.get("a1"));
						// log.info(cache.isElementOnDisk("a1"));
						tx.takeTime("tx", null);
						ToUtil ty = new ToUtil();
						// log.info(cache.get("a9999"));
						// log.info(cache.isElementOnDisk("a9999"));
						ty.takeTime("ty", null);

						EhcacheUtil.removeCache(cacheName);
					}
				});

			}

			// 关闭
			// EhcacheUtil.shutdownCacheManager();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
